package com.trinity.web.controller.report;

import com.trinity.campus.service.ICampusStudentsService;
import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.service.IXcOrdersService;
import com.trinity.specialpay.domain.CampusActivityStudent;
import com.trinity.specialpay.dto.QueryActivityStudentsRequestDto;
import com.trinity.specialpay.service.ICampusActivityStudentService;
import com.trinity.system.service.ISysDeptService;
import com.trinity.web.controller.report.dto.FinancialStatementsResultDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 缴费情况统计
 *
 * @author dsp
 */
@RestController
@RequestMapping("/report/finance")
public class FinancialStatements extends BaseController {

    @Resource
    private ICampusActivityStudentService activityStudentService;
    @Resource
    private IXcOrdersService ordersService;
    @Resource
    private ICampusStudentsService studentsService;
    @Resource
    private ISysDeptService deptService;

    /**
     * 获取订单列表
     */
//    @PreAuthorize("@ss.hasPermi('report:finance:order')")
    @RequestMapping(value = "/order/old", method = {RequestMethod.GET})
    public TableDataInfo list(QueryActivityStudentsRequestDto requestDto) {
        startPage();
        List<CampusActivityStudent> campusActivityStudents = activityStudentService.selectActivityStudentOrderRecordList(requestDto);
        return getDataTable(campusActivityStudents);
    }

    /**
     * 个性化缴费订单
     */
//    @PreAuthorize("@ss.hasPermi('report:finance:order')")
    @Log(title = "个性化缴费订单", businessType = BusinessType.EXPORT)
    @RequestMapping(value = "/order", method = {RequestMethod.GET})
    public AjaxResult list2(XcOrders requestDto) {
        FinancialStatementsResultDto resultDto = new FinancialStatementsResultDto();
        List<XcOrders> xcOrders = ordersService.selectXcOrdersList(requestDto);
        // 总单数
        long totalOder = xcOrders.size();

        // 统计  状态的订单的金额总和
        double totalPrice = xcOrders.stream()
                .mapToDouble(order -> Double.parseDouble(order.getTotalPrice()))
                .sum();

        long totalPayOder = xcOrders.stream()
                .filter(order -> "SUCCESS".equals(order.getStatus()))
                .count();

        // 统计 "SUCCESS" 状态的订单的金额总和
        double payTotalPrice = xcOrders.stream()
                .filter(order -> "SUCCESS".equals(order.getStatus()))
                .mapToDouble(order -> Double.parseDouble(order.getTotalPrice()))
                .sum();
        resultDto.setOrdersList(xcOrders);
        resultDto.setTotalPrice(totalPrice);
        resultDto.setPayTotalPrice(payTotalPrice);
        resultDto.setTotalOder(totalOder);
        resultDto.setTotalPayOder(totalPayOder);
        return success(resultDto);
    }

    /**
     * 统计订单的报告
     */
//    @PreAuthorize("@ss.hasPermi('report:finance:order')")
    @RequestMapping(value = "/total", method = {RequestMethod.GET})
    @Log(title = "统计订单的报告", businessType = BusinessType.EXPORT)
    public AjaxResult total(QueryActivityStudentsRequestDto requestDto) {
        FinancialStatementsResultDto resultDto = new FinancialStatementsResultDto();
        List<CampusActivityStudent> campusActivityStudents = activityStudentService.selectActivityStudentOrderRecordList(requestDto);

        // 统计 "SUCCESS" 状态的订单的金额总和
        double totalPrice = campusActivityStudents.stream()
                .mapToDouble(order -> Double.parseDouble(order.getOrders().getTotalPrice()))
                .sum();

        // 统计 "SUCCESS" 状态的订单的金额总和
        double payTotalPrice = campusActivityStudents.stream()
                .filter(order -> "SUCCESS".equals(order.getOrders().getStatus()))
                .mapToDouble(order -> Double.parseDouble(order.getOrders().getTotalPrice()))
                .sum();
        // 总单数
        long totalOder = campusActivityStudents.size();

        // 支付单数
        long totalPayOder = campusActivityStudents.stream()
                .filter(order -> "SUCCESS".equals(order.getOrders().getStatus()))
                .count();
        resultDto.setTotalPrice(totalPrice);
        resultDto.setPayTotalPrice(payTotalPrice);
        resultDto.setTotalOder(totalOder);
        resultDto.setTotalPayOder(totalPayOder);
        return success(resultDto);
    }


    /**
     * 导出报表
     */
    @Log(title = "导出报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcOrders requestDto) {
        List<XcOrders> xcOrders = ordersService.selectXcOrdersList(requestDto);
        ExcelUtil<XcOrders> util = new ExcelUtil<>(XcOrders.class);
        util.exportExcel(response, xcOrders, "订单数据");
    }

}
